<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>摄像头相关功能</title>
	</head>
	<body>
		<button id="btnBack">返回</button>
		<button id="btnPZ">拍照1</button>
		<button id="btnLX">录像</button>
		
		
		<div class="preview">
			
		</div>
		<script>
			document.addEventListener('plusready', function(){
				//console.log("所有plus api都应该在此事件发生后调用，否则会出现plus is undefined。")
				const preview = document.querySelector('.preview')
				const btnPZ = document.querySelector('#btnPZ')
				const btnLX = document.querySelector('#btnLX')
				const btnBack = document.querySelector('#btnBack')
				
				const cmr = plus.camera.getCamera(2);console.log('cmr=',cmr)
				// 拍照
				btnPZ.onclick = function(){
					
					
					cmr.captureImage((filePath)=>{
						console.log('filePath=',filePath)
						plus.gallery.save( filePath, ()=>{
							plus.nativeUI.toast('保存到相册成功');
						});
						
						// 把地址转成平台地址
						const realPath = plus.io.convertLocalFileSystemURL( filePath )
						console.log('realPath=',realPath)
						const img = new Image()
						img.src = realPath;
						preview.appendChild(img);
					})
				}
				
				// 录像
				btnLX.onclick = function(){
					cmr.startVideoCapture((filePath)=>{
						console.log('filePath=',filePath)
						
						plus.gallery.save( filePath, ()=>{
							plus.nativeUI.toast('保存到相册成功');
						});
						
						const realPath = plus.io.convertLocalFileSystemURL( filePath )
						console.log('realPath=',realPath)
						
						let video = document.createElement('video')
						video.src = realPath;
						// video.autoplay = true;
						video.controls = true
						video.poster = './img/jj.png'
						preview.appendChild(video);
						
						// video.play()
					})
				}
				
				btnBack.onclick = function(){
					const currentWebview = plus.webview.currentWebview()
					plus.webview.close(currentWebview)
				}
				});
		</script>
	</body>
</html>
